package com.whiteape.book.presenter;


import com.whiteape.book.contract.SearchContract;
import com.whiteape.book.core.manager.ExtraEngineManager;
import com.whiteape.book.core.manager.MediaStorageManager;
import com.whiteape.book.core.manager.WebMediaManager;
import com.whiteape.book.model.zssq.remote.RemoteRepository;
import com.whiteape.book.ui.base.RxPresenter;
import com.whiteape.book.utils.LogUtils;
import com.whiteape.book.utils.RxUtils;

import io.reactivex.disposables.Disposable;
import io.reactivex.exceptions.CompositeException;

/**
 * Created by newbiechen on 17-6-2.
 */

public class SearchPresenter extends RxPresenter<SearchContract.View>
        implements SearchContract.Presenter {

    @Override
    public void searchHotWord() {
        Disposable disp = RemoteRepository.getInstance()
                .getHotWords()
                .compose(RxUtils::toSimpleSingle)
                .subscribe(
                        bean -> {
                            mView.finishHotWords(bean);
                        },
                        e -> {
                            LogUtils.e(e);
                        }
                );
        addDisposable(disp);
    }

    @Override
    public void searchKeyWord(String query) {
        //TODO 不用追书神器api

        stopSearching();
        Disposable disp = RemoteRepository.getInstance()
                .getKeyWords(query)
                .compose(RxUtils::toSimpleSingle)
                .subscribe(
                        bean -> {
                            mView.finishKeyWords(bean);
                        },
                        e -> {
                            LogUtils.e(e);
                        }
                );
        addDisposable(disp);
    }

    @Override
    public void stopSearching() {
        if (mDisposable != null && !mDisposable.isDisposed()){
            mDisposable.clear();
        }
    }

    @Override
    public void searchBook(String query, int filteredMediaType) {
        stopSearching();
        addDisposable(MediaStorageManager.getInstance().getUsedSourceCount(filteredMediaType)
                .compose(RxUtils::toSimpleSingle)
                .subscribe(integer -> mView.setUsedSourceCount(integer)));

//        Disposable disposable = ExtraEngineManager.getInstance().searchByExtraEngine(query, null)
//                .compose(RxUtils::toSimpleSingle)
//                .subscribe(beans -> {
//                    mView.finishBooks(beans);
//
//                }, e -> {
//
//                    int errorNum = 1;
//                    if (e instanceof CompositeException){
//                        errorNum = ((CompositeException)e).size();
//                    }
//                    LogUtils.e(e);
//                    mView.errorNums(errorNum);
//                });
//        addDisposable(disposable);
        Disposable disp = WebMediaManager.getInstance().getSearchInfo(query, filteredMediaType)
                .compose(RxUtils::toSimpleFlowable)
                .subscribe(beans -> {
                    mView.finishBooks(beans);
                }, e -> {
                    int errorNum = 1;
                    if (e instanceof CompositeException){
                        errorNum = ((CompositeException)e).size();
                    }
                    LogUtils.e(e);
                    mView.errorNums(errorNum);
                });
        addDisposable(disp);


    }
}
